﻿using System;
using System.Windows;
using System.Linq;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Baosight.ZNH.ZMCL.UI.PopWindows
{
    /// <summary>
    /// 待处理三违—处理页面
    /// </summary>
    /// Title:HandleDeregulation
    /// Service:ServiceZMCL61
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-06-24  高强   [创建]
    ///  
    public partial class TernateHandle : EFViewCmes
    {
        public TernateHandle(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }


        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdDate.IsReadOnly = true;
                    this.fdSourceClassify.IsEnabled = false;
                    this.fdClassify.IsEnabled = false;
                    this.fdResponseDepartment.IsReadOnly = true;
                    this.fdResponsibilityPeople.IsReadOnly = true;
                    this.fdPlace.IsReadOnly = true;
                    this.fdContent.IsReadOnly = true;
                    this.fdCheckPeopleId.IsReadOnly = true;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新负值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            string IsPenaltyValue = this.IsPenalty.Value == null ? "" : this.IsPenalty.Value.ToString();
            string IsTrainingValue = this.IsTraining.Value == null ? "" : this.IsTraining.Value.ToString();

            if ("0".Equals(IsPenaltyValue) || "".Equals(IsPenaltyValue))//给是否罚款赋值
            {
                this.fdIsPenalty.Value = "否";
            }
            else
            {
                this.fdIsPenalty.Value = "是";
            }
            if ("0".Equals(IsTrainingValue) || "".Equals(IsTrainingValue))//给是否培训赋值
            {
                this.fdIsTraining.Value = "否";
            }
            else
            {
                this.fdIsTraining.Value = "是";
            }
            base.OnCommitButtonClick("ZMCL61", "handle", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 重写了确定按钮执行事件的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {

            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                if ("1".Equals(this.IsPenalty.Value.ToString()))
                {
                    EIInfo _eIInfo = new EIInfo();
                    EIRow eIRow = inInfo.GetBlock("result").GetRow(0);

                    TernatePenalty _penalty = new TernatePenalty("insert");//构造一个新的修改页面
                    _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));
                    _eIInfo.AddBlock(inInfo.GetBlock("cmes.cl.penaltyType"));//罚款类型

                    _penalty.EIDataContext = new Platform.Core.UI.EIDataContext();
                    _penalty.EIDataContext.Update(_eIInfo);
                  
                    Collection<EIRow> penaltyType = this.EIDataContext.GetBlock("cmes.cl.penaltyType").GetRows();
                    IEnumerable<EIRow> penaltyTypeResult = from c in penaltyType where c["display"].ToString().IndexOf("现金") >= 0 select c;
                    if (penaltyTypeResult.Count() > 0)
                    {
                        _penalty.fdClassify.Value = penaltyTypeResult.First()["value"].ToString();
                    }

                    _penalty.fdSendnoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                    _penalty.fdTernateRecordId.Value = inInfo.Get("fdTernateRecordId");//三违记录ID
                    _penalty.fdTernateHandleId.Value = eIRow["fdKeyId"];//三违处理ID
                    _penalty.fdSendnoteDepartment.Value = inInfo.Get("fdManageDepartment");//安全主管部门
                    _penalty.fdDepartmentId.Value = eIRow["fdResponseDepartmentId"];//责任部门ID
                    _penalty.fdDepartmentName.Value = eIRow["fdResponseDepartment"];//责任部门

                    EFWindow _popWindow = this.Parent as EFWindow;
                    _popWindow.DialogResult = true;
                    _popWindow.Content = _penalty;
                    _popWindow.Header = "三违处理-罚款";
                }
                else
                {
                    EFWindow window = this.Parent as EFWindow;
                    window.DialogResult = true;
                    window.Close();
                }
            }
        }

        /// <summary>
        /// 获得checkbox的初始状态
        /// </summary>
        /// <param name="info"></param>
        private void GetInitStatus(EIInfo info)
        {
            if (info.GetBlock(EIConstants.ResultBlock).GetRow(0)["fdIsPenalty"].ToString().Equals("是"))
            {
                this.IsPenalty.Value = "1";
            }
            else
            {
                this.IsPenalty.Value = "0";
            }
            if (info.GetBlock(EIConstants.ResultBlock).GetRow(0)["fdIsTraining"].ToString().Equals("是"))
            {
                this.IsTraining.Value = "1";
            }
            else
            {
                this.IsTraining.Value = "0";
            }
        }
    }
}
